/**
 * 
 */
package cn.hbu.zlfw.admin.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.hbu.zlfw.admin.dao.ExpertDao;
import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.Utility;

/**
* @ClassName: ExpertDaoImpl
* @Description: 专家dao接口实现
* @author tiddar
* @date 2018年8月9日
*
*/
@Repository("expertDao")
public class ExpertDaoImpl extends BaseDaoImpl<Expert, String>  implements ExpertDao {

	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.ExpertDao#getExpertByParam(java.util.Map)
	 */
	@Override
	public Expert getExpertByParam(Map<String, Object> params) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("from Expert where ");
		for (Map.Entry<String, Object> param : params.entrySet()) {
			sbf.append(param.getKey()+"=:"+param.getKey()+" and ");
		}
		sbf.append(" delState=:delState ");
		params.put("delState", 0);
		Query query=this.getSession().createQuery(sbf.toString());
		return (Expert) Utility.queryParameterUtil(query, params).uniqueResult();
	}

	/*
	 * 分页条件查询本单位所有专家
	 */
	@Override
	public PageModel<Expert> listAllScienceByParamsToUnit(PageModel<Expert> pageModel, String userName,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("schoolType", schoolType);
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from Expert where schoolType=:schoolType ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		sbfhql.append(" and delState=:delState");
		params.put("delState", 0);
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	/**
	 * 单位管理员查询本单位所有专家信息
	 */
	@Override
	public PageModel<Expert> listExpertByParamsToUnit(PageModel<Expert> pageModel,String userName, String department,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("schoolType", schoolType);
		params.put("delState", 0);	//只查询"未删除"专家
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from Expert where schoolType=:schoolType and delState=:delState  ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		if(department != null && department != "") {
			params.put("department", department);
			sbfhql.append("and department = :department ");
		}
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	@Override
	public Expert getExpertToUpdate(String phoneNumber, String id) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("id", id);
		params.put("phoneNumber", phoneNumber);
		params.put("delState", 0);	//只查询"未删除"专家
		String hql = "from Expert where id != :id and phoneNumber=:phoneNumber and delState=:delState";
		Query query = this.getSession().createQuery(hql);
		return (Expert) Utility.queryParameterUtil(query, params).uniqueResult();
	}

}
